import java.util.*;
import java.util.stream.IntStream;

import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;

public class StreamUseTest {
    public static void main(String[] args) {
        List<Dish> dishList = Arrays.asList(new Dish("beef", false, 700, Type.MEAT),
                new Dish("pork", false, 800, Type.MEAT),
                new Dish("chicken", false, 400, Type.MEAT),
                new Dish("french frice", true, 530, Type.OTHER),
                new Dish("rice", true, 350, Type.OTHER),
                new Dish("season fruit", true, 120, Type.OTHER),
                new Dish("pizza", true, 550, Type.OTHER),
                new Dish("prawns", false, 300, Type.FISH),
                new Dish("salmon", false, 450, Type.FISH)
        );


        List<String> threeHighCaloriesDishNames = dishList.stream()
                .filter((dish) -> dish.getCalories() > 500)
                .map(Dish::getName)
                .limit(3)
                .collect(toList());
        System.out.println(threeHighCaloriesDishNames);
        System.out.println("*****************************************");


        int allCalories = dishList.stream()
                .map(Dish::getCalories)
                .reduce(0, Integer::sum);
        System.out.println(allCalories);
        System.out.println("******************************************");


        List<String> SkipTopTwoCaloryDish = dishList.stream()
                .sorted(Comparator.comparing(Dish::getCalories))
                .skip(2)
                .limit(3)
                .map(Dish::getName)
                .collect(toList());
        System.out.println(SkipTopTwoCaloryDish);
        System.out.println("******************************************");

        String[] words = {"hello", "world"};
        List<String> uniqueCharacter = Arrays.stream(words)
                .map(word -> word.split(""))
                .flatMap(Arrays::stream)
                .distinct()
                .collect(toList());
        System.out.println(uniqueCharacter);
        System.out.println("******************************************");

        Set<String> uniqueCharacter2 = Arrays.stream(words)
                .map(word -> word.split(""))
                .flatMap(Arrays::stream)
                .collect(toSet());
        System.out.println(uniqueCharacter2);
        System.out.println("******************************************");


        boolean containBeef = dishList.stream()
                .map(Dish::getName)
                .anyMatch(s -> s.equals("beef1"));
        System.out.println(containBeef);
        System.out.println("******************************************");

       boolean allmatchtest = dishList.stream()
               .allMatch(dish -> dish.getCalories()<1000);
        System.out.println(allmatchtest);
        System.out.println("******************************************");

        int intStreamTestSum = dishList.stream()
                .mapToInt(Dish::getCalories)
                .sum();
        System.out.println(intStreamTestSum);
        System.out.println("******************************************");

        OptionalInt intStreamTestMax = dishList.stream()
                .mapToInt(Dish::getCalories)
                .max();
        int max = intStreamTestMax.orElse(1);
        System.out.println(max);
        System.out.println("******************************************");

        OptionalDouble intStreamTestAverage = dishList.stream()
                .mapToInt(Dish::getCalories)
                .average();
        double average = intStreamTestAverage.getAsDouble();
        System.out.println(average);
        System.out.println("******************************************");

        List<Integer> numbers = IntStream.rangeClosed(0,10)
                .boxed()
                .collect(toList());
        System.out.println(numbers);
        System.out.println("******************************************");
    }
}
